//
//  engine.cpp
//  gles1
//
//  Created by Realore Realore on 26.09.11.
//  Copyright 2011 Realore Studios. All rights reserved.
//

#include "engine.h"
#include "gles1ViewController.h"
#include <QuartzCore/QuartzCore.h>
#include "EAGLView.h"
#include "pngLoad.h"
#include "sprite.h"
#include "timeengine.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "OGLFT.h"
#include "font.h"
#include "testgame.h"

static const int SCALEWIDTH=640;
static const int SCALEHEIGHT=480;

static int ORIENTATION=0;//0 is default, 
#ifdef STANDART
//standart
static const int WIDTH=480;
static const int HEIGHT=320;
#endif
#ifdef IPAD
//ipad
static const int WIDTH=1024;
static const int HEIGHT=768;
#endif
#ifdef RETINA
//retina
static const int WIDTH=480;
static const int HEIGHT=320;
#endif

//
GLuint Textures[512];

UISprite *Sprites[255][255];

UISprite *Stage;

TEngine *MainTimer;
TEngine *RenderTimer;

Font font("Helvetica",20);

//cursor
bool ScreenTouched;

//struct Cursor{
    float CursorX;
    float CursorY;
//};
UITouch* TouchesArray[10];
int touchesnum=0;

struct Touches {
    bool touched;
    float X;
    float Y;
} TouchStructArr [10];

int CurrentTouchesNum=0;

double elapsedms=0;



double millisecondsquant=0;
double NewQuantTime=0;

void Quant(){
    printf("Quant: %f \n",(float)(CACurrentMediaTime()-millisecondsquant));
    millisecondsquant = CACurrentMediaTime();
    
    
    Stage->Update();
    
    
    
    //DrawFrame();
    /*
    for(int j=0;j<=29;j++){
        for(int i=0;i<=19;i++){
            if(Sprites[i][j] !=nil){
                Sprites[i][j]->Update();
            }
            
        }
    }
*/
}


void HandleInputBegin(UITouch* touch,UIView pview){
    //NSLog(@"touch begin!");
    
    for(int i=0;i<=9;i++){
        if(touch==TouchesArray[i]){
            NSLog(@"touch already in array!");
            return;
        }
    }
    
    for(int ii=0;ii<=9;ii++){
        if(TouchesArray[ii]==nil){
            TouchesArray[ii]=touch;
            NSLog(@"touch %d added",ii);
            return;
        }
    }
}
void HandleInputMove(UITouch* touch,UIView pview){
    //CGPoint currentPoint = [touch locationInView:&pview];
    //NSLog(@"touch moved x: %f  y:%f",currentPoint.x,currentPoint.y);
    
    for(int i=0;i<=9;i++){
        if(touch==TouchesArray[i]){
            //NSLog(@"touch %d moved",i);
            
            CGPoint currentPoint = [touch locationInView:&pview];
            //NSLog(@"touch moved x: %f  y:%f",currentPoint.x,currentPoint.y);
            if(ORIENTATION==1){
                TouchStructArr[i].X = SCALEWIDTH*(WIDTH-currentPoint.y)/WIDTH;
                TouchStructArr[i].Y = (SCALEHEIGHT*currentPoint.x)/HEIGHT;
            }else{     
                TouchStructArr[i].X = SCALEWIDTH-SCALEWIDTH*(WIDTH-currentPoint.y)/WIDTH;
                TouchStructArr[i].Y = SCALEHEIGHT-(SCALEHEIGHT*currentPoint.x)/HEIGHT;

            }
            
            
            TouchStructArr[i].touched = true;
            return;
        }
    }
}

void HandleInputUp(UITouch* touch,UIView pview){
    //NSLog(@"touch end!");
    for(int i=0;i<=9;i++){
        if(touch==TouchesArray[i]){
            TouchesArray[i]=nil;
            TouchStructArr[i].touched = false;
            NSLog(@"touch %d removed", i);
        }
    }
}


//===============
int InitEngine(){
    
    Texture *tempTex = new Texture("Invincible2");
    Texture *tempTex2 = new Texture("singular");
    Texture *tempTex3 = new Texture("logo");
    GLuint Tex = tempTex->Tex;
    
    Stage = new UISprite(2048,1536,0,0,*tempTex3);
    
    UISprite *tempChildsprite = new UISprite(32,16,10,10,*tempTex2);
    
    
    tempChildsprite->Move(0, 0);
 //  
    
    for(int j=0;j<=100;j++){
    for(int i=0;i<=50;i++){
        Sprites[i][j] = new UISprite(32,16,i*32,j*16,*tempTex2);
        Stage->AddChild(Sprites[i][j], 0, Stage);
        Sprites[i][j]->Move(i*32, j*16);
        Sprites[i][j]->GotoAndPlay(0);
    }
    }
    Stage->Update();
    

    RenderTimer = new TEngine(10,DrawFrame);
    MainTimer = new TEngine(40,Quant);
    
    return 1;
}





int anim=0;
int ProcessTouch(){
    
    if(TouchStructArr[0].touched==true){// && TouchStructArr[1].touched==true){
        
        //Sprites[0][0]->drag=true;
        //Sprites[0][0]->StartDrag();
        //Sprites[0][0]->Zoom(TouchStructArr[0].X, TouchStructArr[0].Y, TouchStructArr[1].X, TouchStructArr[1].Y);
        //Sprites[0][0]->Move(TouchStructArr[0].X, TouchStructArr[0].Y);
        //Stage->Move(TouchStructArr[0].X, TouchStructArr[0].Y);
        Stage->StartDrag(TouchStructArr[0].X, TouchStructArr[0].Y);
    }else{
        //Sprites[0][0]->drag=false;
        //        Sprites[0][0]->StopDrag();
        Stage->StopDrag();
    }
    
}

double milliseconds=0;

void DrawFrame(){
    ProcessTouch();
    milliseconds = CACurrentMediaTime();
    //
    SetFrameBuf();
    //
    glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
    glClear(GL_COLOR_BUFFER_BIT);
    

    //projection matrix stuff
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glRotatef(-90,0,0,1);//orientation
    
	glOrthof(0.0, SCALEWIDTH, 0.0, SCALEHEIGHT, 1, -1);//screensize aspect ratio
    
    

	
    anim++;
    if(anim>20)anim = 1;
   
    

    Stage->Draw();

    glLoadIdentity();
    
    //font.print("FPS:",0,0,0);
    char* test;
    
    char str[10];
    int i=100;
        
    sprintf(str,"%d",(int)(1/(CACurrentMediaTime()-milliseconds)));
    //printf("FPS: %d \n",(int)(1/(CACurrentMediaTime()-milliseconds)));
    
    //font.print(str,60,0,0);
    
    PresentFrameBuf();
    //return 1;
}




















